package com.oa.action;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Department;
import com.oa.entity.Post;
import com.oa.entity.User;
import com.oa.service.DepartmentService;
import com.oa.service.LinkService;
import com.oa.service.PostService;
import com.oa.util.NodeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class LinkAction extends ActionSupport implements ModelDriven<User> {
	private static final long serialVersionUID = 1L;
	// 通过spring注入联系人服务类
	@Resource(name = "linkService")
	private LinkService linkService;
	@Resource(name = "postService")
	private PostService postService;
	@Resource(name = "departmentService")
	private DepartmentService departmentService;

	private User user = new User();
	private List<User> userList;

	// 接收前台部门
	private String departmentId;
	// 接收岗位ID
	private String[] postIds;

	// 查询所有联系人
	public String list() {
		List<User> userList = linkService.list();
		ActionContext.getContext().put("userList", userList);
		return "list";
	}

	// 添加页面
	public String addPage() {
		// 准备数据 departmentList 部门列表
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		// 岗位列表
		List<Post> postList = postService.list();
		ActionContext.getContext().put("postList", postList);
		return "savePage";
	}

	// 修改页面LinkAction_updatePage
	public String updatePage() {
		// 准备数据, departmentList
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		// 准备数据, postList
		List<Post> postList = postService.list();
		ActionContext.getContext().put("postList", postList);

		// 准备回显的数据
		User user1 = linkService.getById(user.getUnique_id());
		ActionContext.getContext().put("user1", user1);
		if (user1.getDepartment() != null) {
			departmentId = user1.getDepartment().getUnique_id();
		}
		if (user1.getPostSet() != null) {
			postIds = new String[user1.getPostSet().size()];
			int index = 0;
			for (Post post : user.getPostSet()) {
				postIds[index++] = post.getUnique_id();
			}
		}
		return "savePage";
	} 

	// 添加联系人
	public String add() {

		// 设置所属部门
		user.setDepartment(departmentService.getById(departmentId));
		System.out.println("========="+departmentId);
		// 设置关联的岗位
		List<Post> roleList = postService.getByIds(postIds);
		user.setPostSet(new HashSet<Post>(roleList));
		boolean b1 = linkService.addLink(user);
		// System.out.println(b1+"======");
		if (b1)
			return "toList";
		else
			return "err";
	}

	// 修改联系人
	public String update() {
		//从数据库中取出源对象
		User user1 =linkService.getById(ServletActionContext.
				getRequest().getParameter("unique_id"));
		//设置要修改的属性
		user1.setUser_loginName(user.getUser_loginName());
		user1.setUser_name(user.getUser_name());
		user1.setUser_gender(user.getUser_gender());
		user1.setUser_phoneNumber(user.getUser_phoneNumber());
		user1.setUser_email(user.getUser_email());
		//所属部门
		user1.setDepartment(departmentService.getById(departmentId));
		//所属岗位
		List<Post> postList =postService.getByIds(postIds);
		user1.setPostSet(new HashSet<Post>(postList));
		System.err.println("==================");
		boolean b1 = linkService.updateLink(user1);
		if (b1)
			return "toList";
		else
			return "err";
	}

	// 删除联系人
	public String delete() {
		boolean b1 = linkService.deleteLink(user);
		if (b1)
			return "toList";
		else
			return "err";
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public LinkService getLinkService() {
		return linkService;
	}

	public void setLinkService(LinkService linkService) {
		this.linkService = linkService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// 重新获取模型
	@Override
	public User getModel() {
		return user;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public String[] getPostIds() {
		return postIds;
	}

	public void setPostIds(String[] postIds) {
		this.postIds = postIds;
	}

	public PostService getPostService() {
		return postService;
	}

	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
}
